package cn.melon.mod.store.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import cn.melon.Helper.DBHelper;
import cn.melon.Helper.LoggerHelper;
import cn.melon.base.IBaseExtractor;
import cn.melon.mod.store.dao.GoodsDao;
import cn.melon.mod.store.entity.Goods;
import cn.melon.mod.store.entity.GoodsTable;
import net.ucanaccess.jdbc.UcanaccessSQLException;

/**
 * @ClassName: GoodsDaoImpl
 * @author he
 *    实现接口GoodsDao，Goods数据表的操作。
 */
public class GoodsDaoImpl implements GoodsDao {

    private DBHelper<Goods> dbHelper = new DBHelper<Goods>();

    public Logger goodsDaoImplLogger = LoggerHelper.serverLogger();
    
    /**
     * 用于实现数据库数据转换模式
     */
    private IBaseExtractor<Goods> extractor = new IBaseExtractor<Goods>() {
        @Override
        public List<Goods> convertToList(ResultSet rs) {

            if (rs == null) {
                return null;
            }

            List<Goods> goodsList = new ArrayList<Goods>();
            try {
                while (rs.next()) {
                    Goods goods = new Goods();

                    goods.setGoodsId(rs.getString("goodsId"));
                    goods.setGoodsName(rs.getString("goodsName"));
                    goods.setGoodsType(rs.getString("goodsType"));
                    goods.setGoodsPrice(rs.getDouble("goodsPrice"));
                    goods.setGoodsDescription(rs.getString("goodsDescription"));
                    goods.setGoodsSales(rs.getLong("goodsSales"));
                    goods.setSoldOut(rs.getBoolean("isSoldOut"));

                    goodsList.add(goods);
                }

                // rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return goodsList;
        }
    };

    /*
     * (non-Javadoc)
     * 
     * @see cn.melon.mod.store.dao.GoodsDao#getGoodsById(java.lang.String)
     */
    @Override
    public Goods getGoodsById(String goodsId) {
        String sql = "select * from store_GoodsInfoTable where goodsId = ?";
        String[] paras = new String[] { goodsId };
        Goods goods = null;
        List<Goods> goodsList = null;
        try {
            goodsList = dbHelper.sqlQuery(sql, paras, extractor);
        } catch (UcanaccessSQLException e1) {
            e1.printStackTrace();
        }

        if (goodsList == null) {
            return null;
        }

        goods = goodsList.get(0);

        return goods;
    }

    /* (non-Javadoc)
     * @see cn.melon.mod.store.dao.GoodsDao#getGoodsTable(int, int, java.lang.String)
     */
    @Override
    public GoodsTable getGoodsTable(int start, int num, String sort) {
        // TODO 参数里不能有int
        String sql = null;
        sql = "SELECT * FROM  store_GoodsInfoTable ORDER BY " + sort + " ASC LIMIT " + start + "," + num;
        String[] paras = {};
        GoodsTable goodsTable = null;
        List<Goods> goodsList = null;
        try {
            goodsList = dbHelper.sqlQuery(sql, paras, extractor);

        } catch (UcanaccessSQLException e) {
            e.printStackTrace();
        }

        if (goodsList == null) {
            return null;
        }

        goodsTable = new GoodsTable(goodsList);
        return goodsTable;
    }

    /* (non-Javadoc)
     * @see cn.melon.mod.store.dao.GoodsDao#getGoodsTable(int, int, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public GoodsTable getGoodsTable(int start, int num, String sort, String type, String goodsId, String goodsName) {
        
        goodsDaoImplLogger.info("8" + goodsId + "8");
        String sql = "SELECT * FROM  store_GoodsInfoTable ";
        
        int ss = 0;
        if(!type.equals("全部") || !goodsId.equals("") || !goodsName.equals("")){
            sql += "where ";
        }
        if(!type.equals("全部")){
            sql += "goodsType='" + type + "' ";
            ss++;
        }
        if(!goodsId.equals("")){
            if(ss != 0){
                sql += "and";
            }
            sql += " goodsId='" + goodsId + "' ";
            ss++;
        }
        if(!goodsName.equals("")){
            if(ss != 0){
                sql += "and";
            }
            sql += " goodsName='" + goodsName + "' ";
            ss++;
        }
        
        
        if(sort == ""){
            sql += "ORDER BY goodsId ASC LIMIT ";
        } else{
            sql += "ORDER BY " + sort + " ASC LIMIT ";
        }
        
        sql += start + "," + num;
        goodsDaoImplLogger.info(sql);
        String[] paras = {};
        GoodsTable goodsTable = null;
        List<Goods> goodsList = null;
        try {
            goodsList = dbHelper.sqlQuery(sql, paras, extractor);

        } catch (UcanaccessSQLException e) {
            e.printStackTrace();
        }

        if (goodsList == null) {
            return null;
        }

        goodsTable = new GoodsTable(goodsList);
        return goodsTable;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * cn.melon.mod.store.dao.GoodsDao#addGoods(cn.melon.mod.store.entity.Goods)
     */
    @Override
    public boolean addGoods(Goods goods) {
        String insertSql = "INSERT INTO store_GoodsInfoTable "
                + "(goodsId, goodsName, goodsType, goodsPrice, goodsDescription, goodsSales, isSoldOut)" + "VALUES "
                + "(?, ?, ?, ?, ?, ?, ?)";
        String[] paras = { goods.getGoodsId(), goods.getGoodsName(), goods.getGoodsType(),
                String.valueOf(goods.getGoodsPrice()), goods.getGoodsDescription(),
                String.valueOf(goods.getGoodsSales()), String.valueOf(goods.isSoldOut() ? "1" : "0") };

        return dbHelper.sqlUpdate(insertSql, paras);
    }

    /*
     * (non-Javadoc)
     * 
     * @see cn.melon.mod.store.dao.GoodsDao#removeGoodsById(java.lang.String)
     */
    @Override
    public boolean removeGoodsById(String goodsId) {
        String sqlDelete = "DELETE FROM store_GoodsInfoTable WHERE GoodsId = ?";
        return dbHelper.sqlUpdate(sqlDelete, new String[] { goodsId });
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * cn.melon.mod.store.dao.GoodsDao#updateGoodsInfo(cn.melon.mod.store.entity
     * .Goods)
     */
    @Override
    public boolean updateGoodsInfo(Goods goods) {
        String sqlUpdate = "update store_GoodsInfoTable set "
                + "goodsName=?,goodsType=?,goodsPrice=?,goodsDescription=?,goodsSales=?,isSoldOut=? where goodsId=?";
        String[] paras = { goods.getGoodsName(), goods.getGoodsType(), String.valueOf(goods.getGoodsPrice()),
                goods.getGoodsDescription(), String.valueOf(goods.getGoodsSales()),
                String.valueOf(goods.isSoldOut() ? "1" : "0"), goods.getGoodsId() };
        return dbHelper.sqlUpdate(sqlUpdate, paras);
    }

    @Override
    public boolean changeGoodsPriceById(String goodsId, double price) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean changeIsSoldOutById(String goodsId, boolean isSoldOut) {
        // TODO Auto-generated method stub
        return false;
    }

}
